package com.sinoglobal.ningxia.activity.interestingGame;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.AnswerRule;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.AbstractActivity;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.utils.FlyUtil;

/**
 * @author sbk
 * @createdate 2013-12-12 下午2:17:30
 * @Description: 答题规则
 */
public class AnswerRuleActivity extends AbstractActivity implements
		OnClickListener {

	private TextView tvInfo;
	private Button btnStart;
	private String id;// 活动id
	private boolean isFirst = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.interaction_answer_before_layout);
		init();
		loadData(getString(R.string.Answer_regulationing));
	}

	private void loadData(String msg) {
		btnStart.setEnabled(false);
		new ItktAsyncTaskWithDialog<Void, Void, AnswerRule>(msg, true, false) {
			@Override
			public AnswerRule before(Void... params) throws Exception {
				// Auto-generated method stub
				return RemoteImpl.getInstance().getAnswerRule(id);
			}

			@Override
			public void after(AnswerRule result) {
				btnStart.setEnabled(true);
				if (2 == result.getCode()) { // 超过指定次数
					tvInfo.setText(result.getRule());
					showShortToastMessage(result.getMessage());
					return;
				} else if (Code.CODE_SUCCESS != result.getCode()) {
					showShortToastMessage(result.getMessage());
					return;
				}
				if (isFirst) {
					isFirst = false;
					tvInfo.setText(result.getRule());
					return;
				}
				tvInfo.setText(result.getRule());
				Intent intent = new Intent(AnswerRuleActivity.this,
						AnsweringActivity.class);
				intent.putExtra("id", id);
				FlyUtil.intentForward(AnswerRuleActivity.this, intent);
			}

			@Override
			public void exception() {
				// Auto-generated method stub
				btnStart.setEnabled(true);
			}
		}.execute();
	}

	private void init() {
		DisplayMetrics dm = new DisplayMetrics();
		// 获取屏幕信息
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenWidth = dm.widthPixels;

		int height = (int) (1.136 * screenWidth);

		LinearLayout bgLayout = (LinearLayout) findViewById(R.id.l10);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				screenWidth, height);
		bgLayout.setLayoutParams(params);
		titleView.setText(getString(R.string.crazyAnswer_title));
		templateButtonRight.setVisibility(View.INVISIBLE);
		tvInfo = (TextView) findViewById(R.id.text1);
		btnStart = (Button) findViewById(R.id.bt_id);
		id = getIntent().getStringExtra("id");
		btnStart.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_id:
			loadData(getString(R.string.answering));
			break;

		default:
			break;
		}
	}
}
